package com.mikhail.musicgeneration.texttomusic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.util.Random;

import org.jfugue.*;

public class TextToMusicScaleRelativeNoteChangeGenerator {

	static String[] notes = { "A", "B", "C", "D", "E", "F", "G" };
	static String rest = "R";
	static int octave = 0;
	static String[] durations = { "w", "h", "q", "s", "i" };
	static String input = "";
	static String patternstring1 = "";
	static String patternstring2 = "";
	static String patternstring3 = "";
	static String patternstring4 = "";
	static String patternstring5 = "";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int Action = -1;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while (Action != 2) {

			System.out
					.println("Please select one of the following:\n1. Enter Text to convert to Music.\n2. Quit");

			try {
				Action = Integer.parseInt(br.readLine());
			} catch (NumberFormatException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			if (Action == 2)
				continue;

			System.out
					.println("Hi there,\nPlease input a line of text to generate a line of notes.\n\n>> ");
			try {
				input = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}

			Pattern newpattern = new Pattern();
			Pattern newpattern1 = new Pattern("V0");
			Pattern newpattern2 = new Pattern("V1");
			Pattern newpattern3 = new Pattern("V2");
			Pattern newpattern4 = new Pattern("V3");
			Pattern newpattern5 = new Pattern("V4");

			while (input.length() >= 15) {
				patternstring1 += generateRandomNote();
				input = input.substring(3);
				patternstring2 += generateRandomNote();
				input = input.substring(3);
				patternstring3 += generateRandomNote();
				input = input.substring(3);
				patternstring4 += generateRandomNote();
				input = input.substring(3);
				patternstring5 += generateRandomNote();
				input = input.substring(3);
			}

			newpattern1.add(patternstring1);
			newpattern2.add(patternstring2);
			newpattern3.add(patternstring3);
			newpattern4.add(patternstring4);
			newpattern5.add(patternstring5);

			newpattern.add(newpattern1);
			newpattern.add(newpattern2);
			newpattern.add(newpattern3);
			newpattern.add(newpattern4);
			newpattern.add(newpattern5);

			System.out.println(patternstring1);
			System.out.println(patternstring2);
			System.out.println(patternstring3);
			System.out.println(patternstring4);
			System.out.println(patternstring5);

			Player newplayer = new Player();

			newplayer.play(newpattern);
		}
	}

	public static String generateRandomNote() {

		String temppatternstring = "";

		if (input.length() < 3) {
			return "";
		}

		temppatternstring += notes[(input.charAt(0) % notes.length)]
				+ (input.charAt(1) % 8)
				+ durations[(input.charAt(2) % durations.length)] + " ";

//		 Random r = new Random();
//		 if (r.nextDouble() * 11 < 2.0) {
//		 temppatternstring += rest + durations[r.nextInt(durations.length)] +
//		 " ";
//		 }

		return temppatternstring;
	}

}
